#include "PlayerStateFalling.h"
#include "Player.h"


PlayerStateFalling::PlayerStateFalling(Player* player) : 
PlayerState(player) {}

PlayerStateFalling::~PlayerStateFalling(void){}

void PlayerStateFalling::activate()
{
	_player->getAnimationMgr().setAnimation("falling");
}

void PlayerStateFalling::deactivate()
{

}

int PlayerStateFalling::update()
{
	if (_player->damageReceived())
		return PSRV_TAKE_DAMAGE;
	_player->applyVelocity();
	if ((_player->flags() & JUST_LANDED) == JUST_LANDED)
	{
		_player->removeFlag(JUST_LANDED);
		_player->resetDash();
		return PSRV_LAND;
	}
	else if (_player->getControlState()->isKeyPressed(CControls::Key(CTRL_SHOOT, 0), 0))
		return PSRV_ATTACK_AIR;
	else if (_player->getControlState()->isKeyPressed(CControls::Key(CTRL_DASH, 0), 0) && _player->canDash())
	{
		_player->_velocity.y = 0.0f;
		return PSRV_DASH_AIR;
	}
	else if (_player->getControlState()->isKeyDown(CControls::Key(CTRL_UP, 0), 0) && ((_player->flags() & LADDER_COLLISION) == LADDER_COLLISION))
		return PSRV_CLIMB;


	if (((_player->flags() & COLLISION_HIT_LEFT) == COLLISION_HIT_LEFT && _player->getMovementStateX() == MSX_LEFT) || 
		((_player->flags() & COLLISION_HIT_RIGHT) == COLLISION_HIT_RIGHT && _player->getMovementStateX() == MSX_RIGHT))
	{
		return PSRV_WALL_SLIDING;
	}

	else if (_player->_onGround)
	{
		_player->resetDash();
		return PSRV_LAND;
	}

	return PSRV_NOTHING;
}
